package platformer;

import java.awt.Color;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;

public class Main {

    public static void sleep() {
        try {
            Thread.sleep(20);
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) {
        Scanner scanner;
        ArrayList<String> packs = new ArrayList<String>();
        try {
            scanner = new Scanner(new File("Images/levels/packs.txt"));
            packs = new ArrayList<String>();
            while (scanner.hasNextLine()) {
                packs.add(scanner.nextLine());
            }
        } catch (java.io.IOException e) {
            System.err.println(e.getMessage());
            packs.add("easy");
            packs.add("medium");
            packs.add("hard");
        }
        String[] packlist = new String[packs.size()];
        for (int i = 0; i < packlist.length; i++) {
            packlist[i] = packs.get(i);
        }
        JFrame frame = new JFrame("Platform Game!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GamePanel window = new GamePanel(packlist);
        Panel panel = new Panel();
        final Manager data = new Manager(panel);
        boolean cont = true;
        window.setManager(data);
        panel.setData(data);
        panel.setSize(50, 50);
        panel.setSize(600, 300);
        panel.setBackground(new Color(0, 0, 0));
        frame.setSize(605, 332);
        frame.add(window);
        frame.setVisible(true);
        panel.setVisible(false);
        window.add(panel);
        window.jLabel1.setText("Select a level pack:");
        while (true) {
            cont = true;
            while (cont) {
                if (window.filename != null) {
                    try {
                        scanner = new Scanner(new File("Images/levels/" + window.filename + ".txt"));
                        if (scanner.hasNext()) {
                        	data.platforms = new ArrayList();
                        	data.levels = new ArrayList();
                        	data.hittables = new ArrayList();
                        	data.bgItems = new ArrayList();
                            panel.setVisible(true);
                            window.jComboBox1.setEnabled(false);
                            window.jButton1.setEnabled(false);
                            data.setScanner(scanner);
                            window.add(panel);
                            data.setLevel(0);
                            cont = false;
                        } else {
                            window.filename = null;
                            continue;
                        }
                    } catch (java.io.IOException e) {
                        window.filename = null;
                    }
                }
            }
            while (!data.win) {
                sleep();
                panel.updateStuff();
            }
            panel.setVisible(false);
            window.jComboBox1.setEnabled(true);
            window.jButton1.setEnabled(true);
            window.filename = null;
            window.jLabel1.setText("YOU WON!!! Select a new level pack:");
        }
    }
}
